package com.bingyuan.aipassproject.utils;

import android.media.MediaPlayer;
import android.os.Environment;

import java.io.File;

public class PlayerUtils {

    private MediaPlayer mediaPlayer = new MediaPlayer();

    public void playHit(String fileName){
        try {
            File file = new File(Environment.getExternalStorageDirectory(), fileName);
            mediaPlayer.setDataSource(file.getPath()); //指定音频文件的路径
            mediaPlayer.prepare(); //让MediaPlayer进入到准备状态
            mediaPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void isLoopPlay(String fileName){
        try {
            File file = new File(Environment.getExternalStorageDirectory(), fileName);
            mediaPlayer.setDataSource(file.getPath()); //指定音频文件的路径
            mediaPlayer.prepare(); //让MediaPlayer进入到准备状态
            mediaPlayer.start();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void destoryPlayer(){
        mediaPlayer.stop();
        mediaPlayer.release();
    }
}
